package movee.api.v1;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import movee.domain.api.CountedSuccessResponse;
import movee.domain.dto.BackupDeviceDto;
import movee.domain.query.DownloadFileQuery;
import movee.domain.query.BackupDeviceSearchQuery;
import movee.domain.query.BatchDownloadFileQuery;
import movee.service.AppTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.validation.Valid;
import java.util.List;


/**
 * 设备备份restful controller层
 *
 * @author movee
 */
@RestController
@RequestMapping(value = "/v1/app/task", produces = "application/json")
@Slf4j
@Tag(name = "任务")
public class ApiAppTaskControllerV1 {

    private final AppTaskService deviceService;

    @Autowired
    public ApiAppTaskControllerV1(AppTaskService deviceService) {
        this.deviceService = deviceService;
    }

    /**
     * 查询
     * @param query 查询条件
     * @return 符合条件的表项
     */
    @Operation(summary = "查询")
    @PostMapping(value = "/list")
    public CountedSuccessResponse<List<BackupDeviceDto>> getBackupDeviceList(
            @Parameter(name = "查询条件") @Valid @RequestBody BackupDeviceSearchQuery query) {

        return new CountedSuccessResponse<>(deviceService.getBackupDeviceList(query));
    }

    /**
     * 导出详情
     * @param query 查询条件
     * @return 符合条件的表项
     */
    @Operation(summary = "导出详情")
    @PostMapping(value = "/list/action/export")
    public ResponseEntity<Object> exportExcelFile(
            @Parameter(name = "查询条件") @Valid @RequestBody BackupDeviceSearchQuery query) {

        return deviceService.exportExcelFile(query);
    }

    /**
     * 下载文件
     * @param query 查询条件
     * @return 文件
     */
    @Operation(summary = "下载文件")
    @PostMapping(value = "/config/file/action/download")
    public ResponseEntity<byte[]> downloadSingleFile(
            @Parameter(name = "查询条件") @Valid @RequestBody DownloadFileQuery query) {

        return deviceService.downloadSingleFile(query);
    }

    /**
     * 批量下载文件
     * @param query 查询条件
     * @return 文件
     */
    @Operation(summary = "批量下载文件")
    @PostMapping(value = "/config/file/action/batch/download")
    public ResponseEntity<byte[]> batchDownloadFile(
            @Parameter(name = "查询条件") @Valid @RequestBody BatchDownloadFileQuery query) {

        return deviceService.batchDownloadFile(query);
    }
}
